﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class page_Shop_List : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;                          //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri; //获取决对Url
        isAtFramePageLogin = true;                             //是框架页

        RequestCompetences = Competences.BuildCompetencesList(Competences.Administrator);

        base.OnInit(e);
    }

    #endregion

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.g.CurrentPageIndex = 0;
        this.gPager.PageIndex = 0;

        BindData();
    }       

    protected void g_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string ShopID = e.Item.Cells[0].Text;
        string Day = DateTime.Now.ToString();

        if (e.CommandName == "SalesDetial")         //销售明细 
        {
            Response.Write("<script> parent.AddTab('ShopSaleDetail_List','销售明细','Shop/ShopSaleDetail_List.aspx?ShopID=" + ShopID + "&Day=" + Day + "');</script>");             
        }
        //else if (e.CommandName == "CustomerDetial") //会员明细
        //{
        //    Response.Write("<script> parent.AddTab('CustomerDetail_List','会员明细','page/CustomerDetail_List.aspx?ShopID=" + ShopID + "');</script>");             
        //}
        else if (e.CommandName == "RevenueDetial")  //收入明细
        {
            Response.Write("<script> parent.AddTab('ShopRevenueDetail_List','收入明细','Shop/ShopRevenueDetail_List.aspx?ShopID=" + ShopID + "&Day=" + Day + "');</script>");
        }
    }

    protected void g_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            DataRow dr = drv.Row;
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void g_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            e.Item.SetRenderMethodDelegate(new RenderMethod(DrawGridFooterForG));
        }
    }

    protected void DrawGridFooterForG(HtmlTextWriter output, Control ctl)
    {
        double SumBV = 0;   //总BV（所有查询出的店铺）

        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                SumBV += Shove._Convert.StrToDouble(dt.Rows[i]["PV"].ToString(), 0);
            }

            output.Write("<td align='center'>总计</td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'>" + SumBV + "</td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
        }
    }

    /// <summary>
    /// 默认加载数据到表格
    /// </summary>
    private void BindData()
    {
        dt = new DAL.Views.V_Shop().Open("", "", "id");

        this.g.DataSource = dt;
        this.g.DataBind();
    }

}
